package com.example.mall.service;

import com.example.mall.dao.*;
import com.example.mall.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingService {

    @Autowired
    private CommodityInfoDao commodityInfoDao;
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ContainDao containDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private CommentDao commentDao;

    //下单指定商品
    public Order orderCommodity(Integer aID, Map<Integer, Integer> commoditynumMap , Integer addressID) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = df.format(new Date());
        System.out.println(timestamp);
        Address address = addressDao.getById(addressID);
        Order order = new Order();
        order.setBuyerid(aID);
        order.setLocation(address.getLocation());
        order.setName(address.getName());
        order.setPhonenumber(address.getPhonenumber());
        order.setStatus("未付款");
        order.setTimestamp(timestamp);
        order.setPrice(0);
        orderDao.insert(order);
        Order orderTarget = orderDao.getByTimestamp(timestamp);
        System.out.println(orderTarget);

        int price = 0;
        for (Map.Entry<Integer, Integer> entry : commoditynumMap.entrySet()) {
            CommodityInfo commodityInfo = commodityInfoDao.getById(entry.getKey());
            price += commodityInfo.getPrice() * entry.getValue();
            Contain contain = new Contain();
            contain.setoID(orderTarget.getId());
            contain.setcID(entry.getKey());
            contain.setNum(entry.getValue());
            containDao.insert(contain);
        }

        orderTarget.setPrice(price);
        orderDao.updatePrice(orderTarget);

        return orderTarget;
    }

    //付款指定商品
    public boolean payCommodity(Integer oID) {
        Order order = orderDao.getById(oID);
        if (order == null)
            return false;

        order.setStatus("已付款");
        orderDao.updateStatus(order);
        return true;
    }

    //查看指定用户已付款订单
    public List<Order> getPaiedOrder(Integer aID) {
        List<Order> orderList = orderDao.getByBuyerId(aID);
        for (Order order:orderList) {
            if (!order.getStatus().equals("已付款"))
                orderList.remove(order);
        }
        return orderList;
    }

    //查看指定用户未付款订单
    public List<Order> getUnpaiedOrder(Integer aID) {
        List<Order> orderList = orderDao.getByBuyerId(aID);
        for (Order order:orderList) {
            if (!order.getStatus().equals("未付款"))
                orderList.remove(order);
        }
        return orderList;
    }

    //查看指定用户待退款订单
    public List<Order> getRefundedOrder(Integer aID) {
        List<Order> orderList = orderDao.getByBuyerId(aID);
        for (Order order:orderList) {
            if (!order.getStatus().equals("待退款"))
                orderList.remove(order);
        }
        return orderList;
    }

    //申请退款
    public boolean refund(Integer oID) {
        Order order = orderDao.getById(oID);
        if (order == null)
            return false;

        order.setStatus("待退款");
        orderDao.updateStatus(order);
        return true;
    }

    //取消订单
    public void cancelOrder(Integer oID) {
        orderDao.delete(oID);
    }

    //添加指定商品到购物车
    public void addIntoShoppingCart(Integer aID, Integer cID) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setBuyerid(aID);
        shoppingCart.setcID(cID);
        shoppingCartDao.insert(shoppingCart);
    }

    //删除购物车中的指定商品
    public void deleteFromShoppingCart(Integer aID, Integer cID) {
        shoppingCartDao.delete(aID, cID);
    }

    //查询用户购物车
    public List<CommodityInfo> getShoppingCart(Integer aID) {
        List<ShoppingCart> shoppingCartList = shoppingCartDao.getByBid(aID);
        List<CommodityInfo> commodityInfoList = new ArrayList<>();
        for (ShoppingCart shoppingCart:shoppingCartList) {
            CommodityInfo commodityInfo = commodityInfoDao.getById(shoppingCart.getcID());
            if (commodityInfo != null)
                commodityInfoList.add(commodityInfo);
        }
        return commodityInfoList;
    }

    //评论商品
    public void commentCommodity(Integer aID, Integer cID, String content) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = df.format(new Date());

        Comment comment = new Comment();
        comment.setaID(aID);
        comment.setcID(cID);
        comment.setContent(content);
        comment.setTimestamp(timestamp);
        commentDao.insert(comment);
    }

    //添加收货地址
    public void addAddress(Integer aID, String name, String phoneNumber, String location) {
        Account account = accountDao.getById(aID);
        Integer pID = account.getpID();
        Address address = new Address();
        address.setpID(pID);
        address.setName(name);
        address.setPhonenumber(phoneNumber);
        address.setLocation(location);
        addressDao.insertInfo(address);
    }

}
